<template>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="编号" width="auto"> </el-table-column>

      <el-table-column prop="name" label="书名" width="auto"> </el-table-column>

      <el-table-column prop="author" label="作者" width="auto">
      </el-table-column>

      <el-table-column prop="publish" label="出版社" width="auto">
      </el-table-column>

      <el-table-column prop="price" label="价格" width="auto">
      </el-table-column>

      <el-table-column prop="pages" label="页数" width="auto">
      </el-table-column>

     <el-table-column fixed="right" label="操作" width="300">
        <template #default="scope">
          <el-button @click="edit(scope.row)" type="warning" size="small"
            >修改</el-button
          >
          
          <el-button @click="deteleBook(scope.row)" type="danger"  size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-size="limit"
      layout="prev, pager, next"
      :total="total"
      @current-change="page"
    >
    </el-pagination>

  </div>
</template>

<script>
export default {

  name: "BookManage",
  
  data() {
      return {
        total: null,
        tableData: null,
        limit: 5,
      };
    },

  methods: {
  
     //  更新
    edit(row) {
      this.$router.push({
        path: "/update",
        query: {
          name: row.name,
          id: row.id,
        },
      });
    },

    //  删除
    deteleBook(row) {
      if (confirm("您确定要将:《" + row.name + "》，删除吗") == true) {
        const _this = this;
        axios
          .delete("http://localhost:8181/book/deleteById/" + row.id)
          .then(function (resp) {
            _this.$alert("《" + row.name + "》 删除成功!", "通知：", {
              confirmButtonText: "确定",
              callback: (action) => {
                // 表格重载，以达到数据刷新效果
                window.location.reload();
              },
            });
          });
      } else {
        return false;
      }
    },

    page(currentPage) {
      const _this = this;
      axios
        .get(
          "http://localhost:8181/book/findAll/" + currentPage + "/" + this.limit
        )
        .then(function (resp) {
          _this.tableData = resp.data.content;
          _this.total = resp.data.totalElements;
        });
    },

  },

     // 表格数据初始化
    created() {
      const _this = this;
      axios
        .get("http://localhost:8181/book/findAll/1/" + this.limit)
        .then(function (resp) {
          _this.tableData = resp.data.content;
          _this.total = resp.data.totalElements;
        });
    },


};
</script>

<style>
</style>